package com.appistry.management;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.appistry.api.rest.domain.Credentials;
import com.appistry.api.util.Version;
import com.appistry.management.domain.impl.FabricImpl;
import com.appistry.management.rest.RestFabricManager;
import com.appistry.management.rest.impl.FabricManagerParams;
import com.appistry.management.rest.impl.RestFabricManagerImpl;

public class FabricManager {
	private static final int DEFAULT_HTTP_PORT = 16080;
	private static final int DEFAULT_HTTPS_PORT = 16443;
	private Fabric fabric;
	
	@Deprecated
	public FabricManager(String user, String password, String ip, int port, FabricManagerParams params) throws UnknownHostException {
		InetAddress address = InetAddress.getByName(ip);
		RestFabricManager restFabricManager = new RestFabricManagerImpl(address,port, user, password, params);
		this.fabric = new FabricImpl(restFabricManager);
	}
	
	public FabricManager(String user, String password, String ip, Version version) throws UnknownHostException {
		this(user, password, ip, DEFAULT_HTTP_PORT, false, version);
	}
	
	public FabricManager(String user, String password, String ip, boolean isSSL, Version version) throws UnknownHostException {
		int port = DEFAULT_HTTP_PORT;
		
		if(isSSL){
			port = DEFAULT_HTTPS_PORT;
		}
		
		InetAddress address = InetAddress.getByName(ip);
		Credentials credentials = new Credentials(user, password);
		RestFabricManager restFabricManager = new RestFabricManagerImpl(address, port, isSSL, credentials, version);
		this.fabric = new FabricImpl(restFabricManager);
	}
	
	public FabricManager(String user, String password, String ip, int port, Version version) throws UnknownHostException {
		this(user, password, ip, port, false, version);
	}	
	
	public FabricManager(String user, String password, String ip, int port, boolean isSSL, Version version) throws UnknownHostException {
		InetAddress address = InetAddress.getByName(ip);
		Credentials credentials = new Credentials(user, password);
		RestFabricManager restFabricManager = new RestFabricManagerImpl(address, port, isSSL, credentials, version);
		this.fabric = new FabricImpl(restFabricManager);
	}
	
	public FabricManager(Credentials credentials, InetAddress address, int port, boolean isSSL, Version version) {
		RestFabricManager restFabricManager = new RestFabricManagerImpl(address, port, isSSL, credentials, version);
		this.fabric = new FabricImpl(restFabricManager);
	}


	public Fabric getFabric(){
		return fabric;
	}
}
